package cn.itcast.bos.web.action;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.util.logging.Logger;
import com.opensymphony.xwork2.util.logging.LoggerFactory;

import cn.itcast.bos.domain.take_delivery.Promotion;
import cn.itcast.bos.service.PromotionService;
import cn.itcast.bos.web.action.common.BaseAction;
import cn.itcast.bos.web.action.common.ImageAction;

@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class PromotionAction extends BaseAction<Promotion> {

	@Autowired
	private PromotionService promotionService;

	// 设置日志输出
	private final Logger Log = LoggerFactory.getLogger(ImageAction.class);

	// 设置title图片文件的属性
	private File titleImgFile;
	private String titleImgFileFileName;
	private String titleImgFileContentType;

	public void setTitleImgFile(File titleImgFile) {
		this.titleImgFile = titleImgFile;
	}

	public void setTitleImgFileFileName(String titleImgFileFileName) {
		this.titleImgFileFileName = titleImgFileFileName;
	}

	public void setTitleImgFileContentType(String titleImgFileContentType) {
		this.titleImgFileContentType = titleImgFileContentType;
	}

	@Action(value = "promotion_save", results = {
			@Result(name = "success", type = "redirect", location = "./pages/take_delivery/promotion.html") })
	public String save() throws IOException {
		System.out.println(model);
		// 宣传图 上传、在数据表保存 宣传图路径
		String savePath = ServletActionContext.getServletContext()
				.getRealPath("/upload/");
		String saveUrl = ServletActionContext.getRequest().getContextPath()
				+ "/upload/";

		Log.info("promotion savePath=" + savePath);
		Log.info("promotion saveUrl=" + saveUrl);

		// 生成随机图片名
		UUID uuid = UUID.randomUUID();
		String ext = titleImgFileFileName
				.substring(titleImgFileFileName.lastIndexOf("."));
		String randomFileName = uuid + ext;

		// 保存图片 (绝对路径)
		File destFile = new File(savePath + "/" + randomFileName);
		Log.info(destFile.getAbsolutePath());
		FileUtils.copyFile(titleImgFile, destFile);

		// 将保存路径 相对工程web访问路径，保存model中
		String titleImgPath = saveUrl + randomFileName;
		Log.info("titleImgPath=" + titleImgPath);

		model.setTitleImg(titleImgPath);
		Log.info("titleImg=" + model.getTitleImg());

		// 调用服务层,完成宣传活动数据的保存操作
		promotionService.save(model);

		return SUCCESS;
	}

	private int page;
	private int rows;

	public void setPage(int page) {
		this.page = page;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	@Action(value = "promotion_pageQuery", results = {
			@Result(name = "success", type = "json") })
	public String pageQuery() {
		// 创建pageable对象用来封装page和rows属性
		Pageable pageable = new PageRequest(page - 1, rows);

		Page<Promotion> page = promotionService.pageQuery(pageable);

		System.out.println(page.getContent());
		// 对获取的数据用map集合封装
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("total", page.getTotalElements());
		map.put("rows", page.getContent());

		// 将map存入栈顶
		ActionContext.getContext().getValueStack().push(map);

		return SUCCESS;
	}

}
